﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using Yqd.Enm;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxPlanScheduleController : Controller
    {
        YqdContext ctx;

        public WxPlanScheduleController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var PlanID = Form.GetInt("PlanID");
            var data = ctx.Database.SqlQuery<Yqd_PlanSchedule>("select * from Yqd_PlanSchedule where PlanID={0} order by Instime asc", PlanID);
            var l = WxUser.LogonInfo;
            var ShowAdd = !ctx.Yqd_PlanProgress.Any(it => it.PlanID == PlanID && it.CheckStatus == 审核状态.同意 && it.ProgressStatus == 任务状态.已完成);
            if (ShowAdd)
            {
                ShowAdd = l.Role.Any(it => it == 角色类型.管理员 || it == 角色类型.部门管理员);
                if (ShowAdd)
                {
                    var year = DateTime.Now.Year;
                    var month = DateTime.Now.Month;
                    ShowAdd = !data.Any(it => it.Instime.Year == year && it.Instime.Month == month);
                }
            }
            var users = ctx.Yqd_User.Select(it => new { it.EmployeeCode, it.Name }).ToList();
            return this.ToJson(new
            {
                success = true,
                ShowAdd,
                totalcount = data.Count(),
                data = data.Select(it => new
                {
                    it.ScheduleID,
                    it.Content,
                    year = it.Instime.ToString("yyyy-MM"),
                    users.FirstOrDefault(m => m.EmployeeCode == it.EmployeeCode)?.Name,
                    Instime = it.Instime.ToString("yyyy-MM-dd HH:mm:ss")
                })
            });
        }


        /// <summary>
        /// 添加工作进展
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add(Yqd_PlanSchedule model)
        {
            var SuperviseID = Form.GetInt("SuperviseID");
            var CoordinateID = Form.GetInt("CoordinateID");
            var eSuperviseID = Form.GetInt("eSuperviseID");
            var sSuperviseID = Form.GetInt("sSuperviseID");
            model.ItemID = SuperviseID;
            model.ItemType = 事项类型.钉办督导;
            var IsJb = false;
            if (CoordinateID > 0)
            {
                model.ItemID = CoordinateID;
                model.ItemType = 事项类型.协同管理;
            }
            if (eSuperviseID > 0)
            {
                model.ItemID = eSuperviseID;
                model.ItemType = 事项类型.精准督查;
            }
            if (sSuperviseID > 0)
            {
                IsJb = true;
                model.ItemID = sSuperviseID;
                model.ItemType = 事项类型.专项交办;
            }
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_PlanSchedule.Add(model);
            var ret = ctx.SaveChanges();
            if (ret > 0)
            {
                TaskLogService.AddLog(IsJb ? model.ItemID : model.TaskID, "上报了(" + model.Content + ")任务进度。", IsJb);
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult Delete()
        {
            var ScheduleID = Form.GetInt("ScheduleID");
            var schedule = ctx.Yqd_PlanSchedule.FirstOrDefault(it => it.ScheduleID == ScheduleID);
            var ret = ctx.Database.ExecuteSql("delete from Yqd_PlanSchedule where ScheduleID={0};", ScheduleID);
            if (ret > 0)
            {
                var IsJb = schedule.ItemType == 事项类型.专项交办;
                var TaskID = IsJb ? schedule.ItemID : schedule.TaskID;
                TaskLogService.AddLog(TaskID, "删除了任务进度(" + schedule.Content + ")", IsJb);
            }
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
